import fs from 'fs'

// 定义正式环境文件路径和测试环境文件路径
const devFilePath = './src/config/dev.ts';
const prodFilePath = './src/config/prod.ts';
const cndFilePath = './src/config/cdn.js';
const envTargetFilePath = 'E://huyuanbiao//pc//LL002-ZHR-WEB-FRONTEND//src//config//index.ts';
const cndtargetFilePath = 'E://huyuanbiao//pc//LL002-ZHR-WEB-FRONTEND//node_modules//react-scripts//config//webpack.config.js';

const replace = (lcoalPath, targetPath) => {
  return new Promise((resolve, reject) => {
    // 读取原始文件内容
    fs.readFile(lcoalPath, 'utf8', (err, data) => {
      if (err) {
        console.log('读取原始文件时发生错误:', err);
        reject()
      }

      // 将原始文件内容写入到目标文件中
      fs.writeFile(targetPath, data, 'utf8', (err) => {
        if (err) {
          console.log('写入目标文件时发生错误:', err);
          reject()
        }
        console.log(`${targetPath}:文件替换成功！`);
        resolve()
      });
    });
  })
}

export const replaceCdn = () => {
  return replace(cndFilePath, cndtargetFilePath)
}

export const replaceProd = () => {
  return replace(prodFilePath, envTargetFilePath)
}

export const replaceDev = () => {
  return replace(devFilePath, envTargetFilePath)
}
